﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BUS.DAL
{
    public class DatHangDAL
    {
        internal static int Insert(DatHangBUS datHangBUS)
        {
            ShopDataContext db = DataAccess.Database;

            DatHang dh = new DatHang()
            {
                IDKhachHang = datHangBUS.KhachHang.ID,
                ThoiGianDatHang = DateTime.Now,
                Duyet = false
            };

            db.DatHangs.InsertOnSubmit(dh);
            db.SubmitChanges();

            return dh.ID;
        }

        internal static List<DatHangBUS> GetListWaiting()
        {
            ShopDataContext db = DataAccess.Database;

            var result = from p in db.DatHangs
                         where p.Duyet == false
                         select new DatHangBUS()
                         {
                             ID = p.ID,
                             KhachHang = new KhachHangBUS()
                             {
                                 ID = p.KhachHang.ID,
                                 MaKhachHang = p.KhachHang.MaKhachHang,
                                 HoTen = p.KhachHang.HoTen,
                                 SoDienThoai = p.KhachHang.SoDienThoai,
                                 DiaChi = p.KhachHang.DiaChi,
                                 CMND = p.KhachHang.CMND,
                                 Email = p.KhachHang.Email
                             },
                             ThoiGianDatHang = (DateTime)p.ThoiGianDatHang,
                             Duyet = (bool)p.Duyet,
                             ChiTietDatHangs = ChiTietDatHangDAL.GetByDatHang(p.ID)
                         };

            return result.ToList<DatHangBUS>();
        }

        internal static bool UpdateDuyet(DatHangBUS datHangBUS)
        {
            ShopDataContext db = DataAccess.Database;

            DatHang result = db.DatHangs.SingleOrDefault<DatHang>(p => p.ID == datHangBUS.ID);
            result.Duyet = datHangBUS.Duyet;

            db.SubmitChanges();

            return (bool)result.Duyet;

        }

        internal static List<DatHangBUS> GetListChecked()
        {
            ShopDataContext db = DataAccess.Database;

            var result = from p in db.DatHangs
                         where p.Duyet == true
                         orderby p.KhachHang.MaKhachHang descending
                         select new DatHangBUS()
                         {
                             ID = p.ID,
                             KhachHang = new KhachHangBUS()
                             {
                                 ID = p.KhachHang.ID,
                                 MaKhachHang = p.KhachHang.MaKhachHang,
                                 HoTen = p.KhachHang.HoTen,
                                 SoDienThoai = p.KhachHang.SoDienThoai,
                                 DiaChi = p.KhachHang.DiaChi,
                                 CMND = p.KhachHang.CMND,
                                 Email = p.KhachHang.Email
                             },
                             ThoiGianDatHang = (DateTime)p.ThoiGianDatHang,
                             Duyet = (bool)p.Duyet,
                             ChiTietDatHangs = ChiTietDatHangDAL.GetByDatHang(p.ID)
                         };

            return result.ToList<DatHangBUS>();
        }

        internal static void Delete(DatHangBUS datHangBUS)
        {
            ShopDataContext db = DataAccess.Database;

            DatHang dhDel = db.DatHangs.SingleOrDefault<DatHang>(p => p.ID == datHangBUS.ID);

            db.DatHangs.DeleteOnSubmit(dhDel);
            db.SubmitChanges();
        }
    }
}
